//
//  **** COMPLETE *****
//
//  Init.c
//  ECGVer2
//
//  Created by FRANCIS PAPINEAU on 12-01-01.
//  Copyright 2012 carleton university. All rights reserved.
//
//  This is used as a initialization function set for the Arduino
//  in order for the I/O and the LCDs and storage to work these 
//  functions must be ran no more than one time.
//
//


/* --------------------------------------------------------- */

void initLCD(void){
    
    // Start The Serial Connection
    Serial1.begin(Serial1_BAUD_Rate);
    // Wait for connection to establish
    delay(1000);
}

/* --------------------------------------------------------- */

void initXBee(void){
    clearLCD();
    goToLine(1);
    LCDprint("** INIT XBEE Rx/Tx **");
    // Start The Serial Connection
    Serial.begin(Serial_BAUD_Rate);
    // Wait for connection to establish
    delay(100);
    
    goToLine(2);
    LCDprint("   [OK]    ");
    
    Connection = 'X';
    delay(1000);
}

/* --------------------------------------------------------- */

void initVDIP(void){
    clearLCD();
    
    goToLine(1);
    LCDprint("** INIT VDIP Rx/Tx **");
    
    // set to default BAUD Rate (9600)
    Serial2.begin(SLOW_Serial2_BAUD_Rate);
    
    // set Baud RATE at 115200
    Serial2.print("SBD ");
    Serial2.print(0x1A, BYTE);
    Serial2.print(0x00, BYTE);
    Serial2.print(0x00, BYTE);
    
    // stop the serial connection at 9600
    Serial2.end();
    
    // Start The Serial Connection at 115200
    Serial2.begin(FAST_Serial2_BAUD_Rate);
    // Wait for connection to establish
    delay(100); 
    
    goToLine(2);
    LCDprint("   [OK]    ");
    
    Store = 'S';
    delay(1000);
    
}

/* --------------------------------------------------------- */

void initMenus(void){
    
    // Start && Install the Menu system to the Memory
    initMenusThroughMenus();
}

/* --------------------------------------------------------- */

void initDebugDialog(void){
    clearLCD();
    
    goToLine(1);
    LCDprint("** Init Debug Dialog **");
    
    // Start a Rx/Tx line from PC to send various types of data
    // and to view the status of the device
    goToLine(2);
    LCDprint("Starting Rx/Tx ");
    
    goToLine(3);
    LCDprint("In DEBUG mode ");
    
    goToLine(4);
    LCDprint("   [OK]    ");
    delay(1000);
    
}

/* --------------------------------------------------------- */

void initEEPROM(){
    clearLCD();
    
    goToLine(1);
    LCDprint("** INIT EEPROM **");
    int readVersionID, readSessionID;
    
    // Check the Version ID
    //  If not VersionID -> print the proper ID
    
    readVersionID = EEPROMcheckRevision();
    
    if(readVersionID != VersionID){
        EEPROMwriteRevision(VersionID);
    }
    
    // Check the Session ID
    //  If not SessionID or DEBUG -> print the proper ID (usually +1)
    readSessionID = EEPROMcheckSessionID();
    
    if(readSessionID != SessionID){
        EEPROMwriteSessionID(SessionID);
    }
    goToLine(3);
    LCDprint("   [OK]    ");
    
    delay(1000);
}

/* --------------------------------------------------------- */

void initLEDPins(void){
    clearLCD();
    
    // Setting IR and RED LED Pin modes and original values
    //  -> IR = Output, LOW
    //  -> Red = Output, high
    
    goToLine(1);
    LCDprint("** Init LED **");
    
    pinMode(IrLED, OUTPUT); digitalWrite(IrLED, LOW);
    pinMode(RedLED, OUTPUT); digitalWrite(RedLED, LOW);
    
    goToLine(2);
    LCDprint("  [OK]    ");
    
    delay(1000);
    
}

/* --------------------------------------------------------- */

void initDigitalPins(void){
    clearLCD();
    
    // init buttons as inputs and INT
    goToLine(1);
    
    LCDprint("** Init Digi **");
    
    // the following pins are used by the thermocouple
    // to read in a grey code like temperature
    
    pinMode(VCC, OUTPUT); digitalWrite(VCC, HIGH);
    pinMode(GND, OUTPUT); digitalWrite(GND, LOW);
    
    // turn ON th epull up resistors
    pinMode(OKbutton, INPUT); digitalWrite(OKbutton, HIGH);
    
    pinMode(LoggingPin, INPUT); digitalWrite(LoggingPin, HIGH);
    pinMode(StartupLED, OUTPUT);
    pinMode(PulseOx, INPUT);
    pinMode(ECGPulse, INPUT);
    pinMode(CheckSensors, INPUT);
    
    goToLine(2);
    LCDprint("  [OK]    ");
    delay(1000);

}

/* --------------------------------------------------------- */


void initSensors(void){
    clearLCD();
    
    // check if the sensors have power if so go on 
    // to the mroe interesting stuff
    
    int sensorVoltageReading = 0;
    
    goToLine(1);
    LCDprint("** Init Sensors **");
    
    sensorVoltageReading = digitalRead(CheckSensors);
    
    if(sensorVoltageReading == HIGH){
        
        SensorsOK = true;
        goToLine(2);
        LCDprint("  [OK]    "); 
        delay(1000);
        
    }
    else{
        SensorsOK = false;
        
        goToLine(2);
        LCDprint("Sensor ERROR");
        
        //stop app
        return;
        delay(1000);
        
    }
}

/* --------------------------------------------------------- */

void initbutton(void){
    
    clearLCD();
    
    goToLine(1);
    LCDprint("** Init buttons **");
    reset_buttons();
    
    goToLine(2);
    LCDprint("  [OK]    ");
    delay(1000);
}

/* --------------------------------------------------------- */
/* --------------------------------------------------------- */
/* --------------------------------------------------------- */


